package com.glor.operation.common.web.controller;

import com.glor.operation.common.code.HttpCode;
import com.glor.operation.common.excep.BaseException;
import com.glor.operation.common.utils.ResponseHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by heylear.cai on 2016/2/18.
 */
@Controller
@ControllerAdvice
@Slf4j
public class GlobalController {
    @RequestMapping("/_health_check_")
    public ModelAndView index() {
        return ResponseHelper.buildStateView(HttpCode.HTTP_OK);
    }

    @RequestMapping("/403")
    public ModelAndView error403() {
        return ResponseHelper.buildStateView(HttpCode.HTTP_NO_PERMISSION);
    }

    @RequestMapping("/404")
    public ModelAndView error404() {
        return ResponseHelper.buildStateView(HttpCode.HTTP_NOT_FOUND);
    }

    @RequestMapping("/405")
    public ModelAndView error405() {
        return ResponseHelper.buildStateView(HttpCode.HTTP_NOT_SUPPORT);
    }

    @RequestMapping("/500")
    public ModelAndView error500() {
        return ResponseHelper.buildStateView(HttpCode.HTTP_NOT_AVAILABLE);
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHandler(Exception ex) {
        log.error("error!", ex);
        return ResponseHelper.buildStateView(ex);
    }

    @ExceptionHandler(BaseException.class)
    public ModelAndView exceptionHandler(BaseException ex) {
        return ResponseHelper.buildStateView(ex.getErrorCode(), ex.getMessage());
    }

}
